package com.lvmama.rhino.service.impl;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.Reader;
import java.text.NumberFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.lvmama.rhino.entity.TwoWayTreeNode;
import com.lvmama.rhino.mapper.RelationShipMapper;
import com.lvmama.rhino.pojo.RelationShip;
import com.lvmama.rhino.service.RelationShipService;
import com.lvmama.rhino.utils.PageCodeUtil;


@Service("relationShipService")
public class RelationShipServiceImpl implements RelationShipService {

	@Autowired
    private RelationShipMapper relationShipMapper;

	
    public TwoWayTreeNode queryRelationShip(String pageCode, Date startTime, Date endTime) throws IOException {
    	NumberFormat nf=NumberFormat.getPercentInstance();
    	nf.setMinimumFractionDigits(2);
		pageCode=PageCodeUtil.getPageCode(pageCode, "/a.properties");
	
        List<RelationShip> queryRelationShip = relationShipMapper.queryRelationShip(pageCode, startTime,
                endTime);
        if (queryRelationShip == null || queryRelationShip.size() == 0) {
            return null;
        }
        RelationShip rs = queryRelationShip.get(0);
        List<TwoWayTreeNode> supPageNodes = new ArrayList<TwoWayTreeNode>();
        String supPages = rs.supPage.replaceAll("\\[", "").replaceAll("\\]", "");
        String[] supPagesArr = supPages.split(",");
        int supcount=0;
        for (String supPage : supPagesArr){
        	String[] supPageCount = supPage.split(":");
        	int count=Integer.parseInt(supPageCount[1].trim());
        	supcount+=count;
        };
        for (String supPage : supPagesArr) {
            TwoWayTreeNode node = new TwoWayTreeNode();
            String[] supPageCount = supPage.split(":");
            	String supcode=supPageCount[0].trim();
            	supcode=supcode.replace("\"", "");
            	double count=Double.parseDouble(supPageCount[1].trim());
				node.name =PageCodeUtil.getPageName(supcode, "/a.properties")+ "("+nf.format(count/supcount)+")";
			
            node.isparent = true;
            supPageNodes.add(node);
        }
        List<TwoWayTreeNode> subPageNodes = new ArrayList<TwoWayTreeNode>();
        String subPages = rs.subPage.replaceAll("\\[", "").replaceAll("\\]", "");
        String[] subPagesArr = subPages.split(",");
        int subcount=0;
        for (String subPage : subPagesArr){
        	String[] subPageCount = subPage.split(":");
        	int count=Integer.parseInt(subPageCount[1].trim());
        	subcount+=count;
        };
        for (String subPage : subPagesArr) {
            TwoWayTreeNode node = new TwoWayTreeNode();
            String[] subPageCount = subPage.split(":");
            	String subCode=subPageCount[0].trim();
            	subCode= subCode.replace("\"", "");
            	double count=Double.parseDouble(subPageCount[1].trim());
				node.name = PageCodeUtil.getPageName(subCode, "/a.properties") + "("+nf.format(count/subcount)+")";
		
            subPageNodes.add(node);
        }

        TwoWayTreeNode rootNode = new TwoWayTreeNode();
     
        rootNode.name = PageCodeUtil.getPageName(rs.curPage, "/a.properties");
        rootNode.isparent = true;
        rootNode.parents = supPageNodes;
        rootNode.children = subPageNodes;
        return rootNode;
    }
    

}
